/*
 * Copyright [2011] [Ambud Sharma/ODep Monitor]

   Licensed under the Apache License, Version 2.0 (the "License");

   you may not use this file except in compliance with the License.

   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software

   distributed under the License is distributed on an "AS IS" BASIS,

   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

   See the License for the specific language governing permissions and

   limitations under the License.
 */

package edu.uwsp.orion.odepm.main.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;

import edu.uwsp.orion.odepm.main.shared.JobInfo;

public class JobDeployer extends DialogBox {

	DialogBox box;
	TextBox tbInputPath,tbOutput,tbInput;
	Label lblInputDirectory,lblOutputDirectory;
	
	public JobDeployer() {
		setAnimationEnabled(true);
		box=this;
		
		AbsolutePanel apContainer = new AbsolutePanel();
		apContainer.setSize("765px", "431px");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		apContainer.add(verticalPanel, 0, 0);
		verticalPanel.setSize("768px", "430px");
		
		AbsolutePanel apDBHeader = new AbsolutePanel();
		apDBHeader.setStyleName("joblauncher-header-font");
		verticalPanel.add(apDBHeader);
		apDBHeader.setHeight("23px");
		
		Label label = new Label("ODep Job Deployment Utility");
		label.setStyleName("sendButton");
		
		label.setSize("292px", "29px");
		
		Label lblX = new Label("X");
		lblX.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				box.hide();
			}
		});
		lblX.setStyleName("sendButton");
		apDBHeader.add(lblX, 745, 0);
		
		AbsolutePanel apDepBody = new AbsolutePanel();
		apDepBody.setStyleName("jobLauncherBackGround");
		verticalPanel.add(apDepBody);
		apDepBody.setHeight("408px");
		
		RadioButton rdbtnJob = new RadioButton("new name", "Job");
		rdbtnJob.setEnabled(false);
		rdbtnJob.setStyleName("gwt-Label-JobLauncher");
		apDepBody.add(rdbtnJob, 10, 116);
		
		RadioButton rdbtnJar = new RadioButton("new name", "Jar");
		rdbtnJar.setChecked(true);
		rdbtnJar.setStyleName("gwt-Label-JobLauncher");
		apDepBody.add(rdbtnJar, 68, 116);
		
		RadioButton rdbtnStreaming = new RadioButton("new name", "Streaming(jar)");
		rdbtnStreaming.setEnabled(false);
		rdbtnStreaming.setStyleName("gwt-Label-JobLauncher");
		apDepBody.add(rdbtnStreaming, 124, 116);
		
		Label lblChooseProgramType = new Label("Choose program type:");
		lblChooseProgramType.setStyleName("gwt-Label-JobLauncherHeader-Green");
		apDepBody.add(lblChooseProgramType, 10, 88);
		lblChooseProgramType.setSize("179px", "22px");
		
		lblInputDirectory = new Label("Job Input Directory");
		lblInputDirectory.setStyleName("gwt-Label-JobLauncherHeader");
		apDepBody.add(lblInputDirectory, 295, 104);
		lblInputDirectory.setSize("159px", "26px");
		
		lblOutputDirectory = new Label("Job Output Directory");
		lblOutputDirectory.setStyleName("gwt-Label-JobLauncherHeader");
		apDepBody.add(lblOutputDirectory, 295, 136);
		lblOutputDirectory.setSize("165px", "26px");
		
		tbInput = new TextBox();
		tbInput.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				lblInputDirectory.setStyleName("gwt-Label-JobLauncherHeader-Green");
			}
		});
		apDepBody.add(tbInput, 459, 104);
		tbInput.setSize("227px", "14px");
		
		tbOutput = new TextBox();
		tbOutput.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				lblOutputDirectory.setStyleName("gwt-Label-JobLauncherHeader-Green");
			}
		});
		apDepBody.add(tbOutput, 459, 136);
		tbOutput.setSize("227px", "14px");
		
		Label lblArguments = new Label("Command:");
		apDepBody.add(lblArguments, 10, 372);
		
		AbsolutePanel ap = new AbsolutePanel();
		ap.setStyleName("JobConf");
		apDepBody.add(ap, 10, 194);
		ap.setSize("640px", "165px");
		
		Label lblMapper = new Label("Mapper Count:");
		lblMapper.setStyleName("gwt-Label-JobLauncher");
		ap.add(lblMapper, 10, 43);
		lblMapper.setSize("106px", "19px");
		
		Label lblReducer = new Label("Reducers Count:");
		lblReducer.setStyleName("gwt-Label-JobLauncher");
		ap.add(lblReducer, 193, 43);
		
		IntegerBox integerBox = new IntegerBox();
		integerBox.setEnabled(false);
		ap.add(integerBox, 111, 43);
		integerBox.setSize("45px", "13px");
		
		IntegerBox integerBox_1 = new IntegerBox();
		integerBox_1.setEnabled(false);
		ap.add(integerBox_1, 305, 43);
		integerBox_1.setSize("45px", "13px");
		
		Label lblMapper_1 = new Label("Mapper:");
		lblMapper_1.setStyleName("gwt-Label-JobLauncher");
		ap.add(lblMapper_1, 10, 10);
		lblMapper_1.setSize("63px", "19px");
		
		ListBox comboBox = new ListBox();
		comboBox.setEnabled(false);
		ap.add(comboBox, 63, 10);
		comboBox.setSize("112px", "22px");
		
		Label lblReducer_1 = new Label("Reducer:");
		lblReducer_1.setStyleName("gwt-Label-JobLauncher");
		ap.add(lblReducer_1, 208, 10);
		lblReducer_1.setSize("63px", "19px");
		
		ListBox listBox = new ListBox();
		listBox.setEnabled(false);
		ap.add(listBox, 265, 10);
		listBox.setSize("112px", "22px");
		
		CheckBox chckbxUseCombiner = new CheckBox("Use combiner?");
		chckbxUseCombiner.setEnabled(false);
		chckbxUseCombiner.setStyleName("gwt-Label-JobLauncher");
		ap.add(chckbxUseCombiner, 408, 9);
		
		ListBox listBox_1 = new ListBox();
		listBox_1.setEnabled(false);
		ap.add(listBox_1, 538, 10);
		listBox_1.setSize("80px", "22px");
		
		Button btnLaunch = new Button("Launch");
		btnLaunch.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				JobInfo into=new JobInfo();
				into.inputDir=tbInput.getText();
				into.outputDir=tbOutput.getText();
				JobLauncherAsync async=GWT.create(JobLauncher.class);
				async.launch(into, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						Window.alert("job launched");
					}
					
					@Override
					public void onFailure(Throwable caught) {
						
					}
				});
			}
		});
		apDepBody.add(btnLaunch, 658, 320);
		btnLaunch.setSize("100px", "73px");
		
		Button btnSelect = new Button("Select");
		
		apDepBody.add(btnSelect, 705, 104);
		btnSelect.setSize("53px", "26px");
		
		Button button = new Button("Select");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				invokeDB("output");
			}
		});
		apDepBody.add(button, 705, 136);
		button.setSize("53px", "26px");
		
		Button btnSave = new Button("Save");
		btnSave.setEnabled(false);
		apDepBody.add(btnSave, 658, 231);
		btnSave.setSize("100px", "30px");
		
		TextBox tbCommand = new TextBox();
		tbCommand.setReadOnly(true);
		apDepBody.add(tbCommand, 80, 367);
		tbCommand.setSize("479px", "14px");
		
		final Label lblChooseProgramFile = new Label("Choose/Drop Program file");
		lblChooseProgramFile.setStyleName("gwt-Label-JobLauncherHeader");
		apDepBody.add(lblChooseProgramFile, 10, 6);
		
		ToggleButton tglbtnEnable = new ToggleButton("Edit");
		tglbtnEnable.setEnabled(false);
		apDepBody.add(tglbtnEnable, 575, 367);
		
		Label lblJobConfiguration = new Label("Job Configuration:");
		lblJobConfiguration.setStyleName("gwt-Label-JobLauncherHeader");
		apDepBody.add(lblJobConfiguration, 10, 172);
		lblJobConfiguration.setSize("200px", "19px");
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		apDepBody.add(absolutePanel, 503, 6);
		absolutePanel.setStyleName("dropFileBorder");
		absolutePanel.setSize("213px", "77px");
		
		Label lblDropProgramFile = new Label("Drop Program File here!");
		absolutePanel.add(lblDropProgramFile, 41, 28);
		lblDropProgramFile.setSize("141px", "24px");
		
		Button btnLoad = new Button("Load");
		btnLoad.setEnabled(false);
		apDepBody.add(btnLoad, 658, 196);
		btnLoad.setSize("100px", "30px");
		
		Label lblOr = new Label("OR");
		lblOr.setStyleName("gwt-Label-OR");
		apDepBody.add(lblOr, 354, 29);
		
		RadioButton rdbtnStreamingnonjar = new RadioButton("new name", "Streaming(non-jar)");
		rdbtnStreamingnonjar.setEnabled(false);
		rdbtnStreamingnonjar.setStyleName("gwt-Label-JobLauncher");
		apDepBody.add(rdbtnStreamingnonjar, 10, 142);
		
		RadioButton rdbtnPipes = new RadioButton("new name", "Pipes");
		rdbtnPipes.setEnabled(false);
		rdbtnPipes.setStyleName("gwt-Label-JobLauncher");
		apDepBody.add(rdbtnPipes, 151, 142);
		
		Button btnTrigger = new Button("Trigger");
		btnTrigger.setEnabled(false);
		btnTrigger.setText("Set Trigger");
		apDepBody.add(btnTrigger, 658, 267);
		btnTrigger.setSize("100px", "30px");
		
		final FormPanel formPanel = new FormPanel();
		apDepBody.add(formPanel, 48, 47);
		formPanel.setSize("237px", "26px");
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setAction(GWT.getModuleBaseURL()+"fileupload");
		formPanel.setMethod(FormPanel.METHOD_POST);
		
		final FileUpload fileUpload = new FileUpload();
		fileUpload.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				String fileName=fileUpload.getFilename().replace("\\", "/");
				//Window.alert(fileName);
				
				fileName=fileName.substring(fileName.lastIndexOf("/"));
				//Window.alert(fileName);
				if(fileName.endsWith(".jar") || fileName.endsWith(".zip"))
				{
					fileUpload.setName(fileName);
					formPanel.submit();
					lblChooseProgramFile.setStyleName("gwt-Label-JobLauncherHeader-Green");
				}
				else{
					Window.alert("Only jar or zip files are allowed!");
				}
			}
		});		
		
		formPanel.setWidget(fileUpload);
		fileUpload.setSize("100%", "100%");
		
		
		btnSelect.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				invokeDB("input");				
			}
		});
		this.add(apContainer);
	}
	
	String inputValue;
	
	void invokeDB(final String type){		
		
		final DialogBox db=new DialogBox();
		db.setSize("348px", "263px");
		AbsolutePanel apInputDialog = new AbsolutePanel();
		db.add(apInputDialog);
		apInputDialog.setSize("348px", "263px");
		
		VerticalPanel verticalPanel_1 = new VerticalPanel();
		apInputDialog.add(verticalPanel_1, 0, 0);
		verticalPanel_1.setSize("343px", "267px");
		
		AbsolutePanel apInputDHeader = new AbsolutePanel();
		apInputDHeader.setStyleName("joblauncher-header-font");
		verticalPanel_1.add(apInputDHeader);
		apInputDHeader.setSize("348px", "21px");
		
		Label lblInputDirectory_1 = new Label("Directory Selector");
		apInputDHeader.add(lblInputDirectory_1, 10, 0);
		
		Label lblCloseInputD = new Label("X");
		lblCloseInputD.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				db.hide();
			}
		});
		apInputDHeader.add(lblCloseInputD, 334, 0);
		
		AbsolutePanel apInputD_Container = new AbsolutePanel();
		apInputD_Container.setStyleName("background");
		verticalPanel_1.add(apInputD_Container);
		apInputD_Container.setSize("348px", "244px");
		
		tbInputPath = new TextBox();
		tbInputPath.setReadOnly(true);
		apInputD_Container.add(tbInputPath, 41, 214);
		tbInputPath.setSize("230px", "7px");
		
		Label lblInput = new Label("Path:");
		apInputD_Container.add(lblInput, 10, 214);
		lblInput.setSize("38px", "19px");
		
		final AbsolutePanel apIOContainer = new AbsolutePanel();
		apInputD_Container.add(apIOContainer, 0, 0);
		apIOContainer.setSize("348px", "208px");
		
		LoadFSAsync asyn=GWT.create(LoadFS.class);
		asyn.getDirectories(new AsyncCallback<ArrayList<String>>() {
			
			@Override
			public void onSuccess(ArrayList<String> result) {
				TreePanel trpnlDirectory = new TreePanel();
				
				TreeNode trndroot = new TreeNode("("+result.get(0)+")", "");
				trndroot.setId(result.get(0));
				trpnlDirectory.setRootNode(trndroot);
				trpnlDirectory.setTitle("");
				trpnlDirectory.setEnableDrag(true);
				trpnlDirectory.addListener(new TreePanelListenerAdapter() {
					@Override
					public void onClick(TreeNode node, EventObject e) {
						tbInputPath.setText(node.getPath());			
					}
				});
				
				apIOContainer.add(trpnlDirectory, 0, 0);
				trpnlDirectory.setSize("348px", "208px");
				
				for(int i=1;i<result.size();i++){
					TreeNode treeNode1=new TreeNode("("+result.get(i)+")","");
					treeNode1.setId(result.get(i));
					trndroot.appendChild(treeNode1);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
			}
		});
		
		
		
		Button btnSubmit = new Button("Submit\r\n\r\n`");
		btnSubmit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				inputValue=tbInputPath.getValue();
				if(type.equals("input")){
					tbInput.setText(inputValue);
					lblInputDirectory.setStyleName("gwt-Label-JobLauncherHeader-Green");
				}else{
					tbOutput.setText(inputValue);
					lblOutputDirectory.setStyleName("gwt-Label-JobLauncherHeader-Green");
				}
				db.hide();
			}
		});
		btnSubmit.setText("Submit\r\n\r\n");
		btnSubmit.setStyleName("gwt-Button-Custom");
		apInputD_Container.add(btnSubmit, 287, 214);
		
		db.center();
		
		
	}

}
